local M={}
local Chapter2level_Cfg=_G.CommonOHOH.config.Chapter.chapter2level

-- function M.IsCanUnLock(cid,database)
--     local cfg=Chapter2level_Cfg[cid]
--     local unLockLv=cfg.unLocklevel[1]
--     local nowActiveFriend=M.GetActiveFirend(cid-1,database)
--     local nowLevel=database:GetData("base").level
--     if nowLevel>=unLockLv and nowActiveFriend>=cfg.activeFriend then
--         return  true
--     end
--     return  false
-- end


function M.IsCanUnLock(cid,database)
    
    local cfg=Chapter2level_Cfg[cid]

    local rebirthCount=cfg.condition[2]

    local rebirth=database:GetData("base").rebirth

    if rebirth<rebirthCount then --判断重生数
        -- print("-------updState---------重生次数不够----", rebirth,rebirthCount)
        return false
    end

    local lastChpaterCfg=Chapter2level_Cfg[cid-1]
    if not lastChpaterCfg then
        return true
    end
    local levelId=lastChpaterCfg.levelRange[2] --最后的关卡

    local passCount= database:GetData("levels")[levelId] --通关次数
    passCount=passCount or 0
    -- local str=string.format("章节解锁条件-->章节ID%s, 目标关卡Id%s，目标关卡通关次数%s",cid,levelId,passCount)
    -- print(str)

    if passCount>0  then

        return true
    end

    return false

end



--获取某章节
function M.GetActiveFirend(cid,database)

    local bro=database:GetData("bro").had
    if bro.__real then
        bro=bro.__real
    end
    local count=0
    for k,v in pairs(bro) do
        local arg=string.split(v.baseLevel ,"_")
        if tonumber(arg[1])==cid then
            count=count+1
        end
    
    end
    return count
end


return M